package com.marketingexpert.controller.marketingmgnt.itemrecomment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class GoItemRecommentController implements Controller {

	private String dispatchView;
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		request.setCharacterEncoding("utf8");
		
		String pageNo = request.getParameter("pageNo");
		String forwordUrl = request.getParameter("forwordUrl");
		
		if(pageNo == null) {
			pageNo = "1";
		}
		
		String[] itemIds = request.getParameterValues("itemId");
		String title = request.getParameter("titles");
		String[] titles = new String[itemIds.length];
		if(itemIds.length > 1) {
			titles = title.split(",");
		} else {
			titles[0] = title;
		}
		
		List<Map> _res = new ArrayList<Map>();
		
		for(int i=0; i<itemIds.length; i++) {
			Map<String, String> results = new HashMap<String, String>();
			results.put("itemId", itemIds[i]);
			results.put("title", titles[i]);
			request.getSession().removeAttribute("rec_" + itemIds[i]);
			_res.add(results);
		}
		
		
		request.setAttribute("pageNo", pageNo);
		request.setAttribute("forwordUrl", forwordUrl);
		return new ModelAndView(dispatchView,"results", _res);
	}

	public String getDispatchView() {
		return dispatchView;
	}

	public void setDispatchView(String dispatchView) {
		this.dispatchView = dispatchView;
	}
	
	

}
